package com.example.module_home.View.Fragment;


import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.FragmentUtils;
import com.example.module_base.Base.BaseFragment;
import com.example.module_home.DB.DBOperationHelper;
import com.example.module_home.DB.SeenCodeTable;
import com.example.module_home.Entity.CodeInfo;
import com.example.module_home.Entity.SeenCodeInfo;
import com.example.module_home.Public.ITHouseTables;
import com.example.module_home.Public.ITHouseTables;
import com.example.module_home.R;
import com.example.module_home.View.HomeSeenCodeActivity;
import com.example.module_home.databinding.HomeFragmentPersonalBinding;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.sql.SQLException;
import java.util.List;
import java.util.TreeSet;

/**
 * Created by 陈煜 on 2018/5/21.
 */

public class HMPersonalFragment extends BaseFragment<HomeFragmentPersonalBinding> implements View.OnClickListener{
    private static final String TAG = "HMPersonalFragment";
    private HomeFragmentPersonalBinding mBinding;
    private DBOperationHelper<SeenCodeTable> mDbOperationHelper;
    private boolean isClickSeen;
    private List<SeenCodeTable> mSeenCodes;

    @Override
    public int setlayoutid() {
        return R.layout.home_fragment_personal;
    }


    @Override
    public void init(HomeFragmentPersonalBinding binding) {
        this.mBinding = binding;
        try {
            mDbOperationHelper = new DBOperationHelper<>(mContext,SeenCodeTable.class);
            mSeenCodes = mDbOperationHelper.queryAll();
            if(mSeenCodes.size()!=0){
                mBinding.seenNum.setVisibility(View.VISIBLE);
                mBinding.seenNum.setText(String.valueOf(mSeenCodes.size()));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        EventBus.getDefault().register(this);
        mBinding.setClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id==R.id.con_mine){

        }else if(id==R.id.con_read){
            setupConReadClick();
        }else if(id==R.id.con_collection){

        }else if(id==R.id.con_members){

        }else if(id==R.id.con_setting){

        }
    }

    private void setupConReadClick() {
        isClickSeen = true;
        if(mBinding.seenNum.getVisibility()==View.VISIBLE){
            mBinding.seenNum.setVisibility(View.INVISIBLE);
            //点击之后，表示已查看文章，已看过文章数清零
            ITHouseTables.seenNum = 0;
        }
        Intent intent = new Intent(mContext, HomeSeenCodeActivity.class);
        startActivity(intent);
    }


    @Subscribe(sticky = true,threadMode = ThreadMode.MAIN)
    public void upDateSeenCodeNum(Integer seenNum){
        Log.e(TAG, "upDateSeenCodeNum: seenNum = "+seenNum);
        if(mBinding.seenNum.getVisibility()==View.INVISIBLE){
            mBinding.seenNum.setVisibility(View.VISIBLE);
        }
        mBinding.seenNum.setText(String.valueOf(!isClickSeen?(mSeenCodes.size()+seenNum):seenNum));
    }



    @Override
    public void onResume() {
        Log.e(TAG, "onResume: 恢复");
        super.onResume();
    }

    @Override
    public void onPause() {
        Log.e(TAG, "onPause: 暂停" );
        super.onPause();
    }

    @Override
    public void onStart() {
        Log.e(TAG, "onStart: 开始" );
        super.onStart();
    }

    @Override
    public void onStop() {
        Log.e(TAG, "onStop: 停止");
        super.onStop();
    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "onDestroy: 摧毁");
        super.onDestroy();
        //移除所有的粘性事件
        EventBus.getDefault().removeStickyEvent(Integer.class);
        EventBus.getDefault().unregister(this);
    }
}
